package com.malou.client.util;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.event.dom.client.KeyCodes;

public class Utilities {
	public static Integer itemsPerPage = 22;
	public static Double igv_value = 1.18;
	public static String tienda = null;
	
	public static boolean hasValue(TextField<String> field) {
	    return field.getValue() != null && field.getValue().length() > 0;
	}
	public static boolean hasValue(NumberField field) {
	    return field.getValue() != null && field.getValue().toString().length() > 0;
	}
	public static String toIds(List<BeanModel> list) {
		String ids="";
		for(BeanModel item : list){						
			ids += ","+item.get("id");
		 }
		 ids = ids.substring(1);
		return ids;
	}
	public static String toIdString(List<Integer> list) {
		String ids="";
		for(Integer id : list){						
			ids += ","+id;
		 }
		 ids = ids.substring(1);
		return ids;
	}
	public static List<Integer> toListIds(List<BeanModel> list) {
		List<Integer> retorno = new ArrayList<Integer>();		
		for(BeanModel item : list){			
			retorno.add(Integer.valueOf(item.get("id").toString()));
		 }
		return retorno;
	}
	
	public static void focoEnter(final Component comp, final Component comp2) {

		comp.addListener(Events.KeyDown, new KeyListener() {

			public void componentKeyDown(ComponentEvent event) {
				Integer c = event.getKeyCode();
				if (c == KeyCodes.KEY_ENTER)
					comp2.focus();

			}

		});

	}
	public static void Enter(Component A[]) {
		for (int i = 0; i < A.length - 1; i++)
			focoEnter(A[i], A[i + 1]);

	}

	@SuppressWarnings("unchecked")
	public static void centrarTexto(final Component comp) {

		((Field<Number>) comp).setInputStyleAttribute("textAlign", "center");

	}
	

	public  static Integer diaMes(Integer mes, Integer year) {
		Integer dia = 0;
		if (mes == 2)
			dia = diasFebrero(year);

		else {
			switch (mes) {
			case 1:
				dia = 31;
				break;
			case 3:
				dia = 31;
				break;
			case 4:
				dia = 30;
				break;
			case 5:
				dia = 31;
				break;
			case 6:
				dia = 30;
				break;
			case 7:
				dia = 31;
				break;
			case 8:
				dia = 31;
				break;
			case 9:
				dia = 30;
				break;
			case 10:
				dia = 31;
				break;
			case 11:
				dia = 30;
				break;
			case 12:
				dia = 31;
				break;
			}
		}

		return dia;
	}
	
	private static Integer diasFebrero(Integer year) {
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			return 29;
		else
			return 28;
	}

}
